<?php

include APPLICATION_PATH . "/models/Admin_Group.php";
include APPLICATION_PATH . "/models/Admin.php";
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Admin_AdmingroupController extends Zend_Controller_Action {

    public function init(){       
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->model= new Model_Admin_Group();
        $this->modelMapper= new Model_Admin_GroupMapper();   
        $this->AdminmodelMapper = new Model_AdminMapper();
    }   
    public function addAction(){      
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."admin/admingroup/list";            
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(strlen($_POST["group_name_admin_group"])>0){
                $this->model->setGroup_Name($_POST["group_name_admin_group"]);
            } 
            if(isset($_POST["order_admin_group"])){
                $this->model->setOrder($_POST["order_admin_group"]);
            }
             if(isset($_POST["status_admin_group"])){
                $status_admin_group=1;
            } else {
                $status_admin_group=0;
            }           
            $this->model->setStatus( $status_admin_group);
          
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }     
           $this->view->item= $this->model;
    }
     public function indexAction(){       
    }
     public function editAction(){
        $id = $this->_getParam("id","");        
        $this->view->itemm=$id;
        $redirectUrl=$this->aConfig["site"]["url"]."admin/admingroup/list";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }
        $item=$this->modelMapper->find($id,$this->model);
        $getId=$this->model->getId();
        if(empty($getId)){
            $this->_redirect($redirectUrl);
        }
        if($this->getRequest()->isPost()){
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
             if(strlen($_POST["group_name_admin_group"])>0){
                $this->model->setGroup_Name($_POST["group_name_admin_group"]);
            } 
            if(isset($_POST["order_admin_group"])){
                $this->model->setOrder($_POST["order_admin_group"]);
            }           
            if(isset($_POST["status_admin_group"])){
                $this->model->setStatus($_POST["$status_admin_group"]);
            } 
             if(isset($_POST["status_admin_group"])){
                $status_admin_group=1;
            } else {
                $status_admin_group=0;
            }
           $this->model->setStatus($status_admin_group);             
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);           
        }
        $this->view->item=$this->model;
    }
     public function listAction(){      
       // $this->view->controller = $this;
       // $this->view->item=$this->modelMapper->fetchAll();
    }     
     public function serviceAction(){
        $this->_helper->layout->disableLayout();
        foreach ($this->modelMapper->fetchAll() as $items ) {
            $menber[]=array(
                'Id'=>$items->getId(),
                'group_name'=>$items->getGroup_Name(), 
                'order'=>$items->getOrder(),
                'status'=>$items->getStatus()
            );
        }
        echo json_encode($menber);
        exit();
    }
    
    public  function confirmdeleteAction()
    {
        $id = $this->_getParam("id","0");
        $count = 0;
        foreach($this->AdminmodelMapper->fetchAll() as $item)
        {
            if($item->getAdmin_Group_Id()==$id) $count= $count+1;
        }
        echo $count;
        exit();
    }
    
    public function deleteAction(){
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/admingroup/list";
        $this->modelMapper->delete($id);
        $this->_redirect($redirectUrl);
    }
}


